package com.sxmmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.sxmmall.product.entity.ProductAttrValueEntity;
import com.sxmmall.product.service.ProductAttrValueService;
import com.sxmmall.product.vo.AttrRespVo;
import com.sxmmall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sxmmall.product.entity.AttrEntity;
import com.sxmmall.product.service.AttrService;
import com.sxmmall.common.utils.PageUtils;
import com.sxmmall.common.utils.R;



/**
 * 
 *
 * @author sxm
 * @email 1125400943@qq.com
 * @date 2021-07-06 21:31:04
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /*
     * @Author sxm
     * @Description 获取spu对应基础属性参数
     * @Date 13:26 2021/9/24
     * @Param [spuId]
     * @return [java.lang.Long]
     **/
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> list = productAttrValueService.baseAttrListForSpu(spuId);

        return R.ok().put("data", list);
    }

    /*
     * @Author sxm
     * @Description 修改spu 对应的基本参数
     * @Date 13:30 2021/9/24
     * @Param [spuId]
     * @return [java.lang.Long]
     **/
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId")Long spuId ,@RequestBody List<ProductAttrValueEntity> values){
        productAttrValueService.updateSpuAttr(spuId,values);
        return R.ok();
    }
    /**
     * 列表
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("attrType") String attrType,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
   // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
	//	AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){

		attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
